const service = require('./../service');
const logger = require('./../commons/logger');

exports = module.exports = {
  recordsList: async (ctx, next) => {

    let params = ctx.params || {};

    logger.info('recordsList', params);
    try {
      let result = {
        code: 10000,
        msg: '获取用户信息成功',
        data: {
          page: {
            number: 0,
            size: 20,
            total: 0
          },
          data: []
        }
      };

      if (params.code && params.code.length > 0) {

        let person = await service.getUserInfo(ctx, params.code);

        if (person.code === 10000) {
          person.data.personId = '115d828092e011e788f1a3cf2e185a0e';
          if (person.data && person.data.personId && person.data.personId.length > 0) {
            ctx.session.task = {
              q: {
                page: {
                  size: 20,
                  number: 0
                },
                query: {
                  personId: person.data.personId
                }
              }
            }
            let record = await service.getRecords(ctx, ctx.session.task);
            if (record.code !== 10000) {
              throw new BSPError(record);
            } else {
              logger.debug('recordsList', record.data);
              result = record;
            }
          }
        }
      }

      ctx.status = 200;
      ctx.body = result;
    } catch (e) {
      logger.error('recordsList Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  moreRecords: async (ctx, next) => {
    let query = ctx.session.task || {};

    logger.info('moreRecords', query);
    try {
      query.q.page.number = query.q.page.number + 1;
      let tasks = await service.getRecords(ctx, query);
      if (tasks.code !== 10000) {
        throw new BSPError(tasks);
      } else {
        logger.debug('moreRecords', tasks.data);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '获取参课记录成功', data: tasks.data };
      }
    } catch (e) {
      logger.error('moreRecords Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  getSchedules: async (ctx) => {
    let params = ctx.params || {};
    logger.info('schedule', params);
    ctx.session.schedule = {
      q: {
        page: {
          size: 20,
          number: 0
        },
        query: {
          periodId: params.periodId
        }
      }
    };
    try {
      let schedule = await service.getSchedules(ctx, ctx.session.schedule);
      if (schedule.code !== 10000) {
        throw new BSPError(schedule);
      } else {
        logger.debug('schedule', schedule.data);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '获取日程信息成功', data: schedule.data };
      }
    } catch (e) {
      logger.error('schedule Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  moreSchedules: async (ctx) => {
    let query = ctx.session.schedule || {};
    logger.info('moreSchedules', query);
    try {
      query.q.page.number = query.q.page.number + 1;
      let schedule = await service.getSchedules(ctx, query);
      if (schedule.code !== 10000) {
        throw new BSPError(tasks);
      } else {
        logger.debug('moreRecords', schedule.data);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '获取日程信息成功', data: schedule.data };
      }
    } catch (e) {
      logger.error('moreRecords Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
}
